package com.hs.srm.admin.modules.pricemanagement.controller;


import com.alibaba.fastjson.JSONObject;
import com.hs.srm.admin.modules.pricemanagement.entity.ParityEmail;
import com.hs.srm.admin.modules.pricemanagement.service.ParityEmailService;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.common.utils.DateUtils;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;
import com.hs.srm.tools.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sunnykid.text.SunnykidText;

import javax.mail.MessagingException;
import java.util.*;


/**
 * 发送邮件
 * @author yanfei
 *
 * @date 2019年3月11日
 */
@RestController
@RequestMapping("/parityEmail")
public class ParityEmailController extends AbstractController {

	// 注入业务层
	@Autowired
	private ParityEmailService parityEmailService;

    private static final long serialVersionUID = -4123823577744890422L;
    
    @RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		// 查询用户列表
		PageUtils page = parityEmailService.queryPage(params);
	
		return R.ok(page);
	}
    @RequestMapping("/findById/{inquiryCode}")
	public R findById(@PathVariable("inquiryCode") String inquiryCode ) {
		List<ParityEmail>  parityEmail = parityEmailService.findbyId(inquiryCode);
		//ModelAndView modelAndView = new ModelAndView("modules/pricemanagement/offer/offer_list");
		//modelAndView.addObject("parityEmaillist", parityEmail);
		return R.ok(parityEmail);
	}

	// 报价页面发送邮件
	@RequestMapping(value = "/offerSendMail", produces = "application/json; utf-8")
	@ResponseBody
	// 通过request获取到JSON请求参数
	public R SendMail(@RequestBody JSONObject obj) {
		String data = obj.toJSONString();
		JSONObject jsStr = JSONObject.parseObject(data);
		String str[] =new String[jsStr.getJSONArray("arremail").size()];
		jsStr.getJSONArray("arremail").toArray(str);
		//邮箱
		List<String> arremail = new ArrayList<>(Arrays.asList(str));
		//邮件内容
		String content = jsStr.getString("content");
		//供应商名称
		String vendorName = jsStr.getString("vendorName");
		//发送邮件时间
		String strDate = DateUtils.getTime();
		String email = "";
		for (String item : arremail) {
			email += item;
		}
		ParityEmail parityEmail = new ParityEmail();
		// 报价单，邮件内容
		parityEmail.setEmailContent(content);
		// 邮件发送时间
		parityEmail.setData(DateUtils.stringToDate(strDate, DateUtils.DATE_TIME_PATTERN));
		// 主键id
		parityEmail.setParityEmailId(SunnykidText.getUUID());
		//供应商
		parityEmail.setParityEmailVendorName(vendorName);
		//邮箱
		parityEmail.setParityEmail(email);
		try {
			boolean result=MailUtils.sendMail(arremail,"信息通知",content);
			if (result){
				//记录邮件发送内容
				parityEmailService.insert(parityEmail);
			}else {
				return R.error("false");
			}
		} catch (MessagingException e) {
			e.printStackTrace();
			return R.error("false");
		}
		return R.ok("ok");
	}

}
